package com.jigsawapp.movies.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jigsawapp.movies.R;
import com.jigsawapp.movies.api.ImageLoader;
import com.jigsawapp.movies.api.MoviesApi;
import com.jigsawapp.movies.bean.Movies;


import java.util.List;

/**
 * Created by jigsaw on 2016/5/25.
 * Email: fadeblack307@163.com
 */
public class MovieAdapter extends BaseAdapter implements AbsListView.OnScrollListener{

    private List<Movies> moviesList;
    private LayoutInflater mInflater;
    private ImageLoader mImageLoader;
    private int mStart, mEnd;
    public static String[] URLS;
    private boolean first;


    public MovieAdapter(Context context, List<Movies> moviesList, ListView listView) {
        this.moviesList = moviesList;
        mInflater = LayoutInflater.from(context);
        mImageLoader = new ImageLoader(listView);
        URLS = new String[moviesList.size()];
        for (int i = 0; i < moviesList.size(); i++) {
            URLS[i] = MoviesApi.MOVIES_IMG + moviesList.get(i).getImage();
        }
        first = true;
        listView.setOnScrollListener(this);
    }

    @Override
    public int getCount() {
        return moviesList.size();
    }

    @Override
    public Object getItem(int position) {
        return moviesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.items, null);
            viewHolder.movieImage = (ImageView) convertView.findViewById(R.id.imgView);
            viewHolder.movieTitle = (TextView) convertView.findViewById(R.id.movies_title);
            viewHolder.director = (TextView) convertView.findViewById(R.id.movies_director);
            viewHolder.rating = (TextView) convertView.findViewById(R.id.movies_rating);
            viewHolder.longTime = (TextView) convertView.findViewById(R.id.movies_time);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.movieImage.setImageResource(R.mipmap.ic_launcher);
        String url = MoviesApi.MOVIES_IMG + moviesList.get(position).getImage();
        viewHolder.movieImage.setTag(url);
        mImageLoader.showImageByAsyncTask(viewHolder.movieImage, url);
        viewHolder.movieTitle.setText(moviesList.get(position).getTitle());
        viewHolder.rating.setText(moviesList.get(position).getRating());
        viewHolder.director.setText(moviesList.get(position).getDirector());
        viewHolder.longTime.setText(moviesList.get(position).getLong_time());


        return convertView;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE) {
            mImageLoader.loadImages(mStart, mEnd);
        } else {
            mImageLoader.cancelAllTask();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        mStart = firstVisibleItem;
        mEnd = firstVisibleItem + visibleItemCount;
        if (first && visibleItemCount > 0) {
            mImageLoader.loadImages(mStart, mEnd);
            first = false;
        }
    }

    class ViewHolder {
        public ImageView movieImage;
        public TextView movieTitle;
        public TextView director;
        public TextView rating;
        public TextView longTime;
    }
}
